#include "fuTinyXpath.h"
#include "_tixpath/tinyxml.h"
#include "_tixpath/xpath_static.h"

BEGIN_NAMESPACE_CPPX

using namespace TinyXPath;

struct fuTinyXpath::pimpl_t
{
    TiXmlDocument conf;
};

fuTinyXpath::fuTinyXpath(void) : 
NEW_PIMPL_CLASS()
{
}

fuTinyXpath::~fuTinyXpath(void)
{
}

bool fuTinyXpath::LoadXmlFile( cstr file )
{
    return pimpl_->conf.LoadFile(file);
}

#define NOTNULL(x) ((x) != NULL)

bool fuTinyXpath::LoadXmlString( cstr xmlstr )
{
	return NOTNULL(pimpl_->conf.Parse(xmlstr));
}

std::string fuTinyXpath::ToString( void ) const
{
	if( pimpl_->conf.NoChildren() )
		return "";

	std::string out;
	TiXmlPrinter printer;
	//printer.SetStreamPrinting();
	pimpl_->conf.Accept(&printer);
	out.append(printer.CStr());
	return out;
}

int fuTinyXpath::xpath_intger( const char * cp_xpath_expr )
{
    const TiXmlNode * XNp_source_tree = pimpl_->conf.RootElement();
    if( !XNp_source_tree )
        return 0;
    return i_xpath_int(XNp_source_tree,cp_xpath_expr);
}

std::string fuTinyXpath::xpath_string( const char * cp_xpath_expr )
{
    const TiXmlNode * XNp_source_tree = pimpl_->conf.RootElement();
    if( !XNp_source_tree )
        return "";
    return s_xpath_string(XNp_source_tree,cp_xpath_expr).c_str();
}
//
//#include <vector>
//
//typedef std::vector<const TiXmlNode *> TiXmlNodeList_t;
//
//_CPPX_API bool xpath_node(const TiXmlNode * XNp_source_tree, const char * cp_xpath_expr, const TiXmlNode * & XNp_node);
//_CPPX_API bool xpath_node(const TiXmlNode * XNp_source_tree, const char * cp_xpath_expr, TiXmlNodeList_t & XNp_nodes);
//
//////////////////////////////////////////////////////////////////////////////////
//bool xpath_node(const TiXmlNode * XNp_source_tree,
//                const char * cp_xpath_expr,
//                const TiXmlNode * & XNp_node)
//{
//    xpath_processor xp_proc(XNp_source_tree, cp_xpath_expr);
//    unsigned u_nb = xp_proc.u_compute_xpath_node_set ();
//    if( u_nb == 0 )
//        return false;
//
//    XNp_node = xp_proc.XNp_get_xpath_node (0);
//
//    return xp_proc.e_error == xpath_processor::e_no_error;
//}
//
//bool xpath_node(const TiXmlNode * XNp_source_tree,
//                const char * cp_xpath_expr,
//                TiXmlNodeList_t & XNp_nodes)
//{
//    xpath_processor xp_proc(XNp_source_tree, cp_xpath_expr);
//    unsigned u_nb = xp_proc.u_compute_xpath_node_set ();
//    if( u_nb == 0 )
//        return false;
//
//    XNp_nodes.clear();
//    for( unsigned idx = 0; idx < u_nb; idx++ )
//    {
//        const TiXmlNode * XNp_node = xp_proc.XNp_get_xpath_node (idx);
//        XNp_nodes.push_back(XNp_node);
//    }
//
//    return xp_proc.e_error == xpath_processor::e_no_error;
//}

END_NAMESPACE_CPPX
